<script lang="ts">
import { useDocumentVisibility, useIntervalFn } from '@peng_kai/kit/libs/vueuse';
import { notification } from 'ant-design-vue';

export const forcedHint = useAntdModal(
  defineAsyncComponent(() => import('./ForcedContent.vue')),
  {
    wrapClassName: 'ant-cover__Modal-drawer',
    centered: true,
    footer: true,
  },
);
</script>

<script setup lang="ts">
const appStore = useAppStore();
let lastRefreshTime = Date.now();

function refresh() {
  if ((Date.now() - lastRefreshTime) > 5 * 60 * 1000) {
    appStore.handshakeQry.refetch();
    lastRefreshTime = Date.now();
  }
}

function openUnforcedHint() {
  notification.open({
    class: 'tt-announcement',
    message: '',
    duration: 0,
    description: () => h(defineAsyncComponent(() => import('./UnforcedContent.vue'))),
  });
}

// 0=没有升级; 1=可选升级刷新; 2=强制升级刷新;
watch(() => appStore.handshake?.upgrade, (upgrade) => {
  if (upgrade === 1)
    return openUnforcedHint();

  if (upgrade === 2)
    return forcedHint.open();
}, { immediate: true });

watch(useDocumentVisibility(), v => v && refresh());
useIntervalFn(refresh, 10 * 1000);
</script>

<template>
  <forcedHint.PresetComponent />
</template>
